//
//  ZGLMatchsViewController.m
//  ZeegingLottery
//
//  Created by 李曌 on 2018/10/18.
//  Copyright © 2018 蒂埃里. All rights reserved.
//

#import "ZGLMatchsViewController.h"
#import "ZGLMatchDetailController.h"
#import "ZGLMatchsCell.h"
#import "ZGLMatchModel.h"

@interface ZGLMatchsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *matchListView;
@property (nonatomic, strong) NSMutableArray <ZGLMatchModel *>*matchArray;

@end

@implementation ZGLMatchsViewController

- (NSMutableArray<ZGLMatchModel *> *)matchArray
{
    if (!_matchArray) {
        _matchArray = [NSMutableArray array];
    }
    return _matchArray;
}

- (UITableView *)matchListView
{
    if (!_matchListView) {
        _matchListView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _matchListView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _matchListView.backgroundColor = ViewBackgroundColor;
        _matchListView.delegate = self;
        _matchListView.dataSource = self;
        
        //下拉刷新
        WS(weakself)
        MJRefreshNormalHeader *refreshheader = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [weakSelf getThePredictedResultsList];
            
        }];
        refreshheader.lastUpdatedTimeLabel.hidden = YES;
        
        _matchListView.mj_header = refreshheader;
        
    }
    
    return _matchListView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    //菊花
    [EasyLoadingView showLoadingText:@"正在努力加载中..." config:^EasyLoadingConfig *{
        return [EasyLoadingConfig shared].setLoadingType(LoadingShowTypeIndicator);
    }];
    
    [self.view addSubview:self.matchListView];
    [_matchListView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view);
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.bottom.equalTo(self.view);
//        make.bottom.equalTo(self.view).offset(-STATUEHEIGHT);
        
    }];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        [self getThePredictedResultsList];
        
    });
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.matchArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"MatchsCell";
//    BOOL nibsRegistered = NO;
//    if (!nibsRegistered) {
//        UINib *nib = [UINib nibWithNibName:NSStringFromClass([ZGLMatchsCell class]) bundle:nil];
//        [tableView registerNib:nib forCellReuseIdentifier:ID];
//        nibsRegistered = YES;
//    }
    ZGLMatchsCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [ZGLMatchsCell xibMatchsCell];
    }
    ZGLMatchModel *model = self.matchArray[indexPath.row];
    cell.model = model;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZGLMatchDetailController *detailVC = [[ZGLMatchDetailController alloc]init];
    ZGLMatchModel *model = self.matchArray[indexPath.row];
    detailVC.model = model;
    [self.navigationController pushViewController:detailVC animated:YES];
    
}

#pragma mark - 数据请求
//
- (void)getThePredictedResultsList
{
    WS(weakself);
    [sZGLHTTPRequestManager getThePredictedResultsList:^(id responsedata) {
        
        DLog(@"responseData-%@",responsedata);
        [_matchArray removeAllObjects];
        _matchArray = [ZGLMatchModel mj_objectArrayWithKeyValuesArray:responsedata];
        
        dispatch_async(dispatch_get_main_queue(), ^{
        
            //停止菊花转
            [EasyLoadingView hidenLoading];
            
            [weakSelf.matchListView reloadData];
            [weakSelf.matchListView.mj_header endRefreshing];
        });
        
        
    } Fail:^(ZGLRespondStatusCode bef_status, NSString *bef_message, NSError *error) {
        
    }];
    
}


@end
